package com.example.tao.mycar.utils;

import com.example.tao.mycar.entity.PersonDataEntity;

import java.util.Comparator;

/**
 * Created by Tao on 2016/3/24.
 */
public class Tools {

    /**
     * 获取月份
     * @param datePickerStr
     * @return
     */
    public static String getMonthFromDatePicker(String datePickerStr) {
        String arr[] = datePickerStr.split("-");
        String month = arr[1];
        if (month.length() != 1) {
            char ch = month.charAt(0);
            if (ch == '0') {
                month = month.substring(1,1);
            }
        }
        return month;
    }

    /**
     * 获取日
     * @param datePickerStr
     * @return
     */
    public static String getDayFromDatePicker(String datePickerStr) {
        String arr[] = datePickerStr.split("-");
        String day = arr[2];
        if (day.length() == 1) {
            day = "0" + day;
        }
        return day;
    }

    /**
     * 获取年
     * @param datePickerStr
     * @return
     */
    public static String getYearFromDatePicker(String datePickerStr) {
        String arr[] = datePickerStr.split("-");
        String year = arr[0];
        return year;
    }

    /**
     * 判断字符串str中是否只含有数字或点，如果是返回true，否则返回false
     * @param str
     * @return
     */
    public static boolean isNumericOrDot(String str) {
        for (int i = str.length() - 1; i >=0; i--) {
            if (!Character.isDigit(str.charAt(i)) && !('.' == str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static class SortArrayList implements Comparator {

        @Override
        public int compare(Object lhs, Object rhs) {
            PersonDataEntity data0 = (PersonDataEntity)lhs;
            PersonDataEntity data1 = (PersonDataEntity)rhs;

            return data0.getTime().compareTo(data1.getTime());
        }
    }

}
